// pages/business_settled/business_settled.js
var utils = require('../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var codeName = getApp().globalData.codeName;
var userId = getApp().globalData.userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isImg:false,
    totalprice:0, //费用
    imglist:"", //上传头像
    imageList:"", //上传产品详情图片
    ispos: "leftPos"  //左隐藏
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   var that=this;
   utils.http(httpUrl +"getPrice",{
     code:codeName
   },function(res){
     console.log(res);
     var shopList=[];
     for (var index in res.shop){
      var shoppost={
        id: res.shop[index].id,
        price: res.shop[index].price,
        type:res.shop[index].type,
        value:res.shop[index].value,
        checked:false
      }
      shopList.push(shoppost);
     }
      that.setData({
        shopList:shopList
      })
      console.log(that.data.shopList)
   })
   that.setData({
     modelid: options.modelid,
     categoryId: options.categoryId
   })
   this.getshopType();
  },

  //图片上传
  oNuploadTap: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          tempFilePaths: tempFilePaths[0],
            isImg: true
        })
        wx.uploadFile({
          url: httpUrl + 'uploadImg',
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (e) {
            that.setData({
              imglist: JSON.parse(e.data).imgs
            })
          }

        })
      }
    })
  },

  //产品详情图片上传
  oNproductShlTap:function(e){
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: httpUrl + 'uploadImg',
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (e) {
            that.setData({
              isImg: true,
              imageList: JSON.parse(e.data).imgs
            })
            var listku = that.data.listku;
            if (listku == undefined) {
              listku = []
            }
            listku.push(that.data.imageList);
            that.setData({
              listku: listku
            })
            console.log(listku);
          }

        })
      }
    })
  },
  //获取商家分类列表
  getshopType: function (e) {
    var that = this;
    utils.http(httpUrl + "getmodelList", {
      code: codeName,
      cityid: wx.getStorageSync("city").cityId
    }, function (res) {
      console.log(res);
      that.setData({
        list: res.merchantlist
      })
    })
  },
  //点击商家分类
  clickTypeTap: function (e) {
    this.setData({
      ispos: "rightPos"  //有显示 
    })
  },

  oNlistTap: function (e) {
    var name = e.currentTarget.dataset.name;
    var id = e.currentTarget.dataset.id;
    this.setData({
      modelid: name,
      categoryId: id,
      ispos: "leftPos"  //隐藏
    })

  },

  //商家注册
  formSubmit:function(e){
    var that = this;
    var name=e.detail.value.name;
    var linkman = e.detail.value.linkman
    var telephone = e.detail.value.telephone
    var introduce = e.detail.value.introduce
    var productShl = e.detail.value.productShl
    var imagelist = that.data.listku.join(",");
    utils.http(httpUrl +"intomerchant",{
      code:codeName,
      name: name,//商家名字
      images: that.data.imglist,//头像
      linkman: linkman, //联系人名字
      telephone: telephone, //电话
      introduce: introduce,//商家信息
      location: that.data.locationAddress, //地址
      modelid: that.data.modelid, //分类名称
      userid:userId, //用户提高了
      cityid:wx.getStorageSync("city").cityid, //城市ID
      level: that.data.level,//等级
      categoryId:that.data.categoryId,//分类ID
      price: that.data.totalprice, //价格
      productShl:productShl, //产品介绍
      imageList: imageList   //商家详情图片

      },function(res){
        console.log(res);

        if(res.status==200){
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '失败',
            icon: 'loading',
            duration: 2000
          })
        }
      })
  },

  //地图
  oNlocationTap:function(e){
    var that=this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          locationAddress: res.address
        })
        console.log(res.address);
      }                             
    }) 
  },
  //商家等级选择
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    var info=e.detail.value;
    var infoSplit=info.split("-");
    this.setData({
      level: infoSplit[0],
      totalprice:infoSplit[1]
    } )

  }
})